package cn.action.admin;

import java.io.IOException;
import java.util.Map;

import cn.action.BaseAction;
import cn.beans.Administrator;
import cn.utils.MD5;

import com.opensymphony.xwork2.ActionContext;

@SuppressWarnings("unchecked")
public class LoginAction extends BaseAction {

	private String username;
	private String password;
	private String errorinfo;
	private String msg;
	
	/**
	 * 管理员登陆
	 * @return
	 */
	public String login() throws IOException{
		
		ActionContext act = ActionContext.getContext();
		Map session = act.getSession();
		
		if(null == username || "".equals(username.trim())){
			errorinfo = "用户名不能为空！";
			return INPUT;
		}
		
		if(null == password || "".equals(password.trim())){
			errorinfo = "密码不能为空！";
			return INPUT;
		}
		
		Administrator admin = commonservice.findById(username, new Administrator());
		if(null == admin){
			errorinfo = "不存在该用户！";
			return INPUT;
		}else{
			String md5Password = MD5.getMD5ofStr(password);
			if(admin.getPassword().equals(md5Password)){
				
				session.put("adminId", admin.getUsername());
				session.put("adminName", admin.getName());
				return SUCCESS;
			}else{
				errorinfo = "密码错误，请新输入！";
				return INPUT;
			}
		}
		
	}
	
	/**
	 * 管理员退出登陆
	 * @return
	 */
	public String logout(){
		Map session = ActionContext.getContext().getSession();
		session.clear();
		return SUCCESS;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getErrorinfo() {
		return errorinfo;
	}

	public void setErrorinfo(String errorinfo) {
		this.errorinfo = errorinfo;
	}
	
}
